/**
 * @author Pawel Gadecki <pidzama@gmail.com>
 * 
 */
package spwodne;

import java.awt.AWTKeyStroke;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.swing.*;
import org.jdesktop.layout.GroupLayout;

public class OWplata extends JFrame {

    public OWplata(MTWplaty mtw, OEKlient oek) {
        this.oek = oek;
        this.mtw = mtw;
        initComponents();
        st = new String[3];
        sdf = new SimpleDateFormat("dd-MM-yyyy");
        String s = sdf.format(new Date());
        st = s.split("-");
        txtDay.setText(st[0]);
        txtMonth.setText(st[1]);
        txtYear.setText(st[2]);
    }

    private void initComponents() {
        Set secik = new HashSet();
        secik.add(AWTKeyStroke.getAWTKeyStroke(10, 0));
        setFocusTraversalKeys(0, secik);
        jPanel1 = new JPanel();
        jLabel1 = new JLabel();
        txtDay = new JTextField();
        jLabel2 = new JLabel();
        jLabel3 = new JLabel();
        txtMonth = new JTextField();
        txtYear = new JTextField();
        jLabel4 = new JLabel();
        txtKasa = new JTextField();
        jLabel5 = new JLabel();
        jButton1 = new JButton();
        jButton2 = new JButton();
        setDefaultCloseOperation(2);
        jPanel1.setBorder(BorderFactory.createTitledBorder("Nowa Wp\u0142ata"));
        jLabel1.setText("Data");
        txtDay.setText("12");
        txtDay.setNextFocusableComponent(txtMonth);
        txtDay.setSelectionEnd(2);
        jLabel2.setText("-");
        jLabel3.setText("-");
        txtMonth.setText("12");
        txtMonth.setNextFocusableComponent(txtYear);
        txtYear.setText("2004");
        txtYear.setNextFocusableComponent(txtKasa);
        jLabel4.setText("Kwota");
        txtKasa.setHorizontalAlignment(11);
        txtKasa.setText("0.0");
        txtKasa.setNextFocusableComponent(jButton1);
        jLabel5.setText("[z\u0142]");
        GroupLayout jPanel1Layout = new GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(1).add(jPanel1Layout.createSequentialGroup().addContainerGap().add(jPanel1Layout.createParallelGroup(1).add(jPanel1Layout.createSequentialGroup().add(jLabel1).addPreferredGap(0).add(txtDay, -2, -1, -2).add(4, 4, 4).add(jLabel2).add(4, 4, 4).add(txtMonth, -2, -1, -2).add(3, 3, 3).add(jLabel3)).add(jPanel1Layout.createSequentialGroup().add(jLabel4).addPreferredGap(0).add(txtKasa, -2, 48, -2))).add(jPanel1Layout.createParallelGroup(1).add(jPanel1Layout.createSequentialGroup().addPreferredGap(0).add(jLabel5)).add(jPanel1Layout.createSequentialGroup().add(3, 3, 3).add(txtYear, -2, -1, -2))).addContainerGap(-1, 32767)));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(1).add(jPanel1Layout.createSequentialGroup().addContainerGap().add(jPanel1Layout.createParallelGroup(3).add(jLabel1).add(txtDay, -2, -1, -2).add(jLabel2).add(txtMonth, -2, -1, -2).add(jLabel3).add(txtYear, -2, -1, -2)).add(22, 22, 22).add(jPanel1Layout.createParallelGroup(3).add(jLabel4).add(jLabel5).add(txtKasa, -2, -1, -2)).addContainerGap(-1, 32767)));
        jButton1.setText("OK");
        jButton1.setNextFocusableComponent(jButton2);
        jButton1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
            final OWplata this$0;
            

            {
                this$0 = OWplata.this;
            //super();
            }
        });
        jButton2.setText("anuluj");
        jButton2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
            final OWplata this$0;
            

            {
                this$0 = OWplata.this;
            //super();
            }
        });
        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(1).add(layout.createSequentialGroup().addContainerGap().add(layout.createParallelGroup(1).add(jPanel1, -2, -1, -2).add(2, layout.createSequentialGroup().add(jButton1, -2, 72, -2).addPreferredGap(0).add(jButton2))).addContainerGap(-1, 32767)));
        layout.setVerticalGroup(layout.createParallelGroup(1).add(layout.createSequentialGroup().addContainerGap().add(jPanel1, -2, -1, -2).addPreferredGap(0).add(layout.createParallelGroup(3).add(jButton2).add(jButton1)).addContainerGap(-1, 32767)));
        pack();
    }

    private void jButton2ActionPerformed(ActionEvent evt) {
        dispose();
    }

    private void jButton1ActionPerformed(ActionEvent evt) {
        try {
            String s = (new StringBuilder()).append(txtDay.getText()).append("-").append(txtMonth.getText()).append("-").append(txtYear.getText()).toString();
            Date d = sdf.parse(s);
            String money = txtKasa.getText();
            money.replace(",", ".");
            Float k = Float.valueOf(Float.parseFloat(money));
            mtw.addRow(k.floatValue(), d);
            oek.odswiezPodsumowanie();
            oek.mtn.fireTableDataChanged();
            dispose();
        } catch (ParseException exc) {
            JOptionPane.showMessageDialog(this, "B\u0142ad podczas wprowadzania daty - (sprawdz czy pola daty zawieraja odpowiednie warto\u015B\u0107i)");
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "B\u0142ad podczas wprowadzania danych - (sprawdz czy pola liczbowe nie zawieraj\u0105 liter \noraz u\u017Cywaj kropki do oddzielenia cz\u0119\u015Bci u\u0142amkowej)");
        }
    }
    private MTWplaty mtw;
    private OEKlient oek;
    private SimpleDateFormat sdf;
    private String st[];
    private JButton jButton1;
    private JButton jButton2;
    private JLabel jLabel1;
    private JLabel jLabel2;
    private JLabel jLabel3;
    private JLabel jLabel4;
    private JLabel jLabel5;
    private JPanel jPanel1;
    private JTextField txtDay;
    private JTextField txtKasa;
    private JTextField txtMonth;
    private JTextField txtYear;
}
